package spl.army;

import java.util.Formatter;

public class Item {
	private ItemContainer container;
	private int amount;
	private Sergeant consumer;
	
	/**  
	 * @param containerParam holds the item locking object
	 * @param amountParam as instances of the item that is allocated
	 * @param consumerSergeant of the item
	 */
	public Item(ItemContainer containerParam, int amountParam, Sergeant consumerSergeant)
	{
		this.container = containerParam;
		this.amount = amountParam;
		this.consumer = consumerSergeant;
	}

	/**
	 * Returns the item to the warehouse
	 * Note: synchronized on the container, so this function blocks.
	 */
	public void returnToWarehouse() {
		this.container.returnAmount(this.amount, this.consumer);
		this.amount = 0;
	}

	/**
	 * @return string repr of the item
	 */
	public String toString() {
		StringBuilder sb = new StringBuilder();
		Formatter formatter = new Formatter(sb);
		
		return formatter.format("Item: %s (%d/%d)",
				this.container.toString(), this.amount, this.container.getAmount()).toString();
	}

}
